package com.xb.loan.usercenter.user.user.service.impl;

import com.xb.loan.log.exception.BizException;
import com.xb.loan.usercenter.service.user.IUserSysMsgService;
import com.xb.loan.usercenter.service.user.bean.UserSysMsgVO;
import com.xb.loan.usercenter.user.user.dao.UserInfoDao;
import com.xb.loan.usercenter.user.user.dao.UserMsgDao;
import com.xb.loan.usercenter.user.user.dao.UserSysMsgDao;
import com.xb.loan.usercenter.user.user.dao.bean.UserSysMsg;
import com.xb.loan.util.cache.redis.comm.FormatOperation;
import com.xb.loan.util.common.ObjectOperation;
import com.xb.loan.util.mybatis.MyTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userSysMsgService")
public class UserSysMsgServiceImpl implements IUserSysMsgService {
	
	@Autowired
	UserSysMsgDao userSysMsgDao;
	
	@Autowired
	UserMsgDao userMsgDao;
	
	@Autowired
    UserInfoDao userInfoDao;

	@MyTransactional
	@Override
	public void add(UserSysMsgVO userSysMsgVO) throws BizException {
		
		UserSysMsg userSysMsg = ObjectOperation.convertByClasses(userSysMsgVO, UserSysMsg.class);
		String sysId = FormatOperation.assemblySeq("common", 4, FormatOperation.formatDateToYMDHms(new Date()), "");
		userSysMsg.setId(sysId);
		userSysMsgDao.insertSelective(userSysMsg);
		
		/*添加系统消息时，不再循环拷贝至用户消息表
		for(int i=100;i<=109;i++){
	        Map<String, Object> map = new HashMap<String, Object>();
	        map.put("sysId", sysId);
	        map.put("type", userSysMsgVO.getType());
	        map.put("title", userSysMsgVO.getTitle());
	        map.put("content", userSysMsgVO.getContent());
	        map.put("imgUrl", userSysMsgVO.getImgUrl());
	        map.put("tableId", i);
			userMsgDao.insertBatch(map);
		}
		*/
	}

	@Override
	public void delete(String id) throws BizException {
		
		userSysMsgDao.delete(id);
		/*删除系统消息时，不再循环删除用户消息表
		for(int i=100;i<=109;i++){
	        Map<String, Object> map = new HashMap<String, Object>();
	        map.put("sysId", id);
	        map.put("tableId", String.valueOf(i));
			userMsgDao.deleteBatch(map);
		}
		*/
	}

	@Override
	public void update(UserSysMsgVO userSysMsgVO) throws BizException {
		
		UserSysMsg userSysMsg = ObjectOperation.convertByClasses(userSysMsgVO, UserSysMsg.class);
		userSysMsgDao.update(userSysMsg);
		
		/*更新系统消息时，不再循环更新用户消息表
		for(int i=100;i<=109;i++){
	        Map<String, Object> map = new HashMap<String, Object>();
	        map.put("type", userSysMsgVO.getType());
	        map.put("title", userSysMsgVO.getTitle());
	        map.put("content", userSysMsgVO.getContent());
	        map.put("imgUrl", userSysMsgVO.getImgUrl());
	        map.put("updateTime", true);
	        map.put("sysId", userSysMsgVO.getId());
	        map.put("tableId", i);
			userMsgDao.updateBatch(map);
		}
		*/
	}

	@Override
	public UserSysMsgVO queryById(String id) throws BizException {
		
		UserSysMsg userSysMsg =  userSysMsgDao.queryById(id);
		return ObjectOperation.convertByClasses(userSysMsg, UserSysMsgVO.class);
	}

	@Override
	public List<UserSysMsgVO> queryUserSysMsg(Integer type, String title, String content,
                                              String startTime, String endTime, int pageNum, int numPerPage) throws BizException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("title", title);
		map.put("content", content);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		map.put("pageNum", pageNum);
		map.put("numPerPage", numPerPage);
		List<UserSysMsg> userSysMsgList =  userSysMsgDao.queryUserSysMsg(map);
		return ObjectOperation.convertByClassesList(userSysMsgList, UserSysMsgVO.class);
	}
	
	@Override
	public int queryUserSysMsgCount(Integer type, String title, String content,
                                    String startTime, String endTime) throws BizException {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("title", title);
		map.put("content", content);
		map.put("startTime", startTime);
		map.put("endTime", endTime);
		return userSysMsgDao.queryUserSysMsgCount(map);
	}

}
